我试图从 sidebarPanel 访问 mainPanel 中的输入字段,但我无法成功。
代码:
shinyUI(pageWithSidebar{
sidebarPanel(
sliderInput("x", "X", min = 10, max = 100, value = 50)
),
mainPanel(
#this is where I wanna use the input from the sliderInput
#I tried input.x, input$x, paste(input.x)
)
})
似乎问题出在哪里?或者不能在 mainPanel 中使用来自 sidebarPanel 的输入?
最佳答案
您只能使用服务器端的输入。
例如 :
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel("test"),
sidebarPanel(
sliderInput("x", "X", min = 10, max = 100, value = 50)
),
mainPanel(
verbatimTextOutput("value")
)
),
server = function(input, output, session) {
output$value <- renderPrint({
input$x
})
}
))
编辑::
动态设置绘图的尺寸。
使用 renderUi 使用输入值渲染绘图输出。
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel("Test"),
sidebarPanel(
sliderInput("width", "Plot Width (%)", min = 0, max = 100, value = 100),
sliderInput("height", "Plot Height (px)", min = 0, max = 400, value = 400)
),
mainPanel(
uiOutput("ui")
)
),
server = function(input, output, session) {
output$ui <- renderUI({
plotOutput("plot", width = paste0(input$width, "%"), height = paste0(input$height, "px"))
})
output$plot <- renderPlot({
plot(1:10)
})
}
))
关于r Shiny : access input fields in UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423363/