Приведенный ниже код MWE работает должным образом, за исключением того, что вывод таблицы данных не отображается на главной панели при нажатии кнопки действия «Векторные значения» на вкладке «По балансам» (первая вкладка, которая отображается по умолчанию) .
На данный момент я хотел бы отобразить таблицу в базе Shiny без использования пакета таблиц, такого как DT.
Я не думаю, что указанная ниже функция vectorsAll
необходима, вместо нее я пробовал использовать функцию yield()
, и она все равно не работает.
Что я делаю не так? Это должна быть такая простая вещь, визуализация таблицы данных из 60 строк, я уверен, что упускаю из виду что-то очень очевидное.
функция vectorPlot, которая соответствует MWE ниже:
vectorPlot <- function(w,x,y,z){plot(w,main=x,xlab=y,ylab=z,type="b",col="blue",pch=19,cex=1.25)}
MWE:
library(shiny)
library(shinyMatrix)
library(shinyjs)
button2 <- function(x,y){actionButton(x,y,style="width:90px;margin-bottom:5px;font-size:80%")}
matrix1Input <- function(x){
matrixInput(x,
value = matrix(c(0.2), 4, 1, dimnames = list(c("A","B","C","D"),NULL)),
rows = list(extend = FALSE, names = TRUE),
cols = list(extend = FALSE, names = FALSE, editableNames = FALSE),
class = "numeric")}
vectorBase <- function(x,y){
a <- rep(y,x)
b <- seq(1:x)
c <- data.frame(x = b, y = a)
return(c)}
ui <-
pageWithSidebar(
headerPanel("Model..."),
sidebarPanel(
fluidRow(helpText(h5(strong("Base Input Panel")),align="center",
style="margin-top:-15px;margin-bottom:5px")),
# Panels rendered with uiOuput & renderUI in server to stop flashing at invocation
uiOutput("Panels")
), # close sidebar panel
mainPanel(
tabsetPanel(
tabPanel("By balances", value=2,
fluidRow(h5(strong(helpText("Select model output to view:")))),
fluidRow(
button2('showVectorPlotBtn','Vector plots'),
button2('showVectorValueBtn','Vector values'),
), # close fluid row
div(style = "margin-top: 5px"),
# Shows outputs on each page of main panel
uiOutput('showResults'),
), # close tab panel
tabPanel("By accounts", value=3),
tabPanel("Liabilities module", value=4),
id = "tabselected"
) # close tabset panel
) # close main panel
) # close page with sidebar
server <- function(input,output,session)({
base_input <- reactive(input$base_input)
showResults <- reactiveValues()
yield <- function(){vectorBase(60,input$base_input[1,1])} # Must remain in server section
# --- Conditional panels rendered here rather than in UI to eliminate invocation flashing ---------->
output$Panels <- renderUI({
tagList(
conditionalPanel(
condition="input.tabselected==2",
matrix1Input("base_input"),
div(style = "margin-top: 0px"),
useShinyjs(),
), # close conditional panel
conditionalPanel(condition="input.tabselected==3"),
conditionalPanel(condition="input.tabselected==4")
) # close tagList
}) # close renderUI
# --- Below produces vector plots as default view when first invoking App ----------------------------->
output$graph1 <-renderPlot(vectorPlot(yield(),"A Variable","Period","Rate"))
# --- Below produces vector plots after having clicked "Vector Plot" button; see above for pre-click ->
observeEvent(input$showVectorPlotBtn,
{showResults$showme <-
tagList(plotOutput("graph1"))
},ignoreNULL = FALSE)
# --- Below produces vector values table ------------------------------------------------------------->
vectorsAll <- reactive({cbind(Period = 1:60,Yld_Rate = pct(yield()[,2]))})
output$table1 <- renderTable({vectorsAll()})
observeEvent(input$showVectorValueBtn,{showResults$showme <- show("table1")})
# --- Below sends both vector plots and vector values to UI section above ---------------------------->
output$showResults <- renderUI({showResults$showme})
}) # close server
shinyApp(ui, server)
Я получаю сообщение об ошибке при попытке запустить MWE:
Error: could not find function "vectorPlot"
.Я боялся, что пропущу функцию! Вот vectorPlot: vectorPlot <- function(w,x,y,z){plot(w,main=x,xlab=y,ylab=z,type="b",col="blue",pch=19 ,секс=1,25)}
Попробуйте
observeEvent(input$showVectorValueBtn,{showResults$showme <- tableOutput("table1")})
Спасибо YBS, это работает. Так что моя ошибка заключалась в том, что я использовал show("table1") вместо tableOutput("table1"). Также я упустил еще одну пользовательскую функцию «pct» в исходном MWE, который я опубликовал. Я отвечу на вопрос, используя ваше исправление, и укажу все функции, необходимые для правильной работы.