![](/img/trans.png)
[英]How to display multiple rows in the table from plotly graphs using R Shiny and delete the last row from the table?
[英]Multiple line graphs using plotly in shiny R
我是 shiny R 和 Plotly 的新手。 我正在嘗試構建一個具有兩個下拉框的儀表板,我們通過這些下拉框和 plot Plotly 圖表進行輸入。 所有數據集都有時間、溫度和重量列。 時間在 x 軸上,對於 y 軸,我們可以 select 溫度或重量或兩者兼而有之。
第一個下拉菜單將輸入到 select 的數據集。
第二個下拉框將輸入 select 來自所選數據集中的變量。 然而,我已經弄清楚的大多數事情,y 軸 label 不會動態變化。 label 正在獲取(輸入$變量)而不是溫度或重量。
這是 shiny r output這里也是可重復的示例和我的代碼
library(shiny)
library(plotly)
library(DT)
df1 <- data.frame("time" = 1:10, "temp" = c(21,15,31,12,23,45,67,34,54,10), "weight" = c(10,20,30,40,65,35,68,89,100,23), stringsAsFactors = FALSE)
df2 <- data.frame("time" = 1:10, "temp" = c(31,65,31,22,23,45,67,54,54,45), "weight" = c(30,20,40,40,65,85,68,89,14,24), stringsAsFactors = FALSE)
ui <- fluidPage(
titlePanel( div(column(width = 5, h2('title here')), )),
# Input: Selector for choosing dataset
selectInput(inputId = "dataset",
label = "Choose a dataset:",
choices = c("df1","df2")),
selectInput(inputId = "variable",
label = "Variable selection",
choices = c("temp","weight"),
selected = "weight",
multiple = FALSE),
mainPanel(
# Output
tabsetPanel(type = "tabs",
tabPanel("Plot", plotlyOutput('plot')),
tabPanel("Data", DT::dataTableOutput("table")),
tabPanel("Key_metrics", DT::dataTableOutput("Key_metrics")))
)
)
server <- function(input, output) {
dataDf <- reactive({
temp <- get(input$dataset)
})
output$plot <- renderPlotly(
plot_ly(dataDf(), x = ~time, y =~get(input$variable), type = 'scatter', mode = 'lines', name = "temp") %>%
add_trace(dataDf(), x = ~time, y = ~weight, type = 'scatter', mode = 'lines',name = "weight")
)
output$table <- DT::renderDataTable({
dataDf()
})
output$Key_metrics <- DT::renderDataTable({
})
}
shinyApp(ui,server)
您可以在layout()
中指定軸標簽。 請注意, xaxis
和yaxis
需要一個列表作為參數(有關詳細信息,請參見此處):
output$plot <- renderPlotly(
plot_ly(dataDf(), x = ~time, y =~get(input$variable), type = 'scatter', mode = 'lines', name = "temp") %>%
add_trace(dataDf(), x = ~time, y = ~weight, type = 'scatter', mode = 'lines',name = "weight") %>%
layout(xaxis = list(title = "Time"), yaxis = list(title = input$variable))
)
編輯:在評論之后,這里是如何 plot 如果選擇了兩個變量,則為兩行,否則為另一行(不要忘記在selectInput()
中放置multiple = TRUE
:
library(shiny)
library(plotly)
library(DT)
df1 <- data.frame("time" = 1:10, "temp" = c(21,15,31,12,23,45,67,34,54,10), "weight" = c(10,20,30,40,65,35,68,89,100,23), stringsAsFactors = FALSE)
df2 <- data.frame("time" = 1:10, "temp" = c(31,65,31,22,23,45,67,54,54,45), "weight" = c(30,20,40,40,65,85,68,89,14,24), stringsAsFactors = FALSE)
ui <- fluidPage(
titlePanel( div(column(width = 5, h2('title here')), )),
# Input: Selector for choosing dataset
selectInput(inputId = "dataset",
label = "Choose a dataset:",
choices = c("df1","df2")),
selectInput(inputId = "variable",
label = "Variable selection",
choices = c("temp","weight"),
selected = "weight",
multiple = TRUE),
mainPanel(
# Output
tabsetPanel(type = "tabs",
tabPanel("Plot", plotlyOutput('plot')),
tabPanel("Data", DT::dataTableOutput("table")),
tabPanel("Key_metrics", DT::dataTableOutput("Key_metrics")))
)
)
server <- function(input, output) {
dataDf <- reactive({
temp <- get(input$dataset)
})
output$plot <- renderPlotly({
if (length(input$variable) > 1){
plot_ly(dataDf(), x = ~time, y =~get(input$variable[1]),
type = 'scatter', mode = 'lines', name = "temp") %>%
add_trace(dataDf(), x = ~time, y = ~get(input$variable[2]),
type = 'scatter', mode = 'lines',name = "weight") %>%
layout(xaxis = list(title = "Time"))
}
else {
plot_ly(dataDf(), x = ~time, y =~get(input$variable[1]), type = 'scatter', mode = 'lines', name = "temp") %>%
add_trace(dataDf(), x = ~time, y = ~get(input$variable[1]), type = 'scatter', mode = 'lines',name = "weight") %>%
layout(xaxis = list(title = "Time"), yaxis = list(title = input$variable))
}
})
output$table <- DT::renderDataTable({
dataDf()
})
output$Key_metrics <- DT::renderDataTable({
})
}
shinyApp(ui,server)
根據原始答案將您想要的內容作為 y 軸 label 。 請注意,此答案僅在有兩種選擇時才有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.