我不熟悉RStudio Shiny中的应用程序构建。我已经检查了以下代码中的所有内容,并且似乎可以一直进行到绘图为止,但是我无法获取任何绘图,并且服务器窗口显示错误:“找不到对象'Inputs2'。”任何帮助将不胜感激与代码。我的最终目标是获得情节。
library(shiny)
library(Benchmarking)
library(plotly)
# load data
maindata <- read.csv("raw.csv", stringsAsFactors = FALSE)
# Define UI for application
ui <- fluidPage(theme = "bootstrap.css",
# Application title
titlePanel("Preferred Usage"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
h3("Enter Inputs",""),
numericInput("qout1", "Output 1","0"),
numericInput("qout2", "Output 2","0"),
numericInput("qin1", "Input 1","0"),
numericInput("qin2", "Input 2","0"),
numericInput("qin3", "Input 3","0"),
numericInput("pin1", "Price Input 1","0"),
numericInput("pin2", "Price Input 2","0"),
numericInput("pin3", "Price Input 3","0"),
actionButton("runButton", "Run Analysis")
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("coolplot")
)
)
)
服务器端代码:
server <- function(input, output) {
# The important part of reactiveValues()
values <- reactiveValues()
grph <- reactiveValues()
values$df <- maindata
addData <- observe({
# your action button condition
if(input$runButton > 0) {
# create the new line to be added from your inputs
newLine <- isolate(c(input$qout1, input$qout2, input$qin1, input$qin2, input$qin3, input$pin1, input$pin2, input$pin3))
# update your data
# note the unlist of newLine, this prevents a bothersome warning message that the rbind will return regarding rownames because of using isolate.
table1 <- isolate(values$df <- rbind(unlist(newLine), as.matrix(values$df)))
# inputs and outputs for analysis
y <- data.frame(table1[, 1:2])
x <- data.frame(table1[, 3:5])
w <- data.frame(table1[, 6:8])
# function to do some analysis
xopt <- cost.opt(x, y, w, RTS=1)
# Fetch results
results <- data.frame(xopt[["xopt"]])
results[1,]
#Prepare data for plotting
Inputs <- c("Seed", "Fertilizer", "Power")
Inputs2 <- as.numeric(c(x[1,]))
Inputs3 <- as.numeric(c(results[1,]))
isolate(grph$try1 <- rbind(Inputs, Inputs2, Inputs3))
grph$try1 <- data.frame(t(grph$try1))
}
})
output$coolplot <- renderPlotly({p <- plot_ly(data.frame(grph$try1), x = ~Inputs, y = ~Inputs2, type = 'bar', text = round(Inputs2, 2), textposition = 'auto', name = 'Inputs2', marker = list(color = 'rgb(204,204,204)')) %>%
add_trace(y = ~Inputs3, text = round(Inputs3, 2), textposition = 'auto', name = 'Inputs3', marker = list(color = 'rgb(49,130,189)')) %>%
layout(title = "Plot", barmode = 'group', yaxis = list(title = "Count"))})
}
# Run the application
shinyApp(ui = ui, server = server)
上述工作中的可复制数据样本:
1-前5行的“ raw.csv”
structure(list(qout1 = c(6.278, 7.345, 6.609, 6.141, 8.653),
qout2 = c(4.399, 5.423, 4.787, 3.947, 5.492), qin1 = c(1.719,
3.422, 2.585, 2.308, 2.717), qin2 = c(0.891, 1.663, 1.018,
1.484, 1.54), qin3 = c(0.906, 0.742, 0.826, 1.406, 1.621),
pin1 = c(1.798, 0.675, 0.743, 1.76, 1.774), pin2 = c(3.377,
2.871, 4.857, 3.19, 3.283), pin3 = c(6.42, 8.997, 9.13, 6.136,
6.205)), row.names = c(NA, 5L), class = "data.frame")
2-服务器端代码中的“ table1”数据,前五行。由于新输入数据全为零,因此所有变量的第一行均为零:
structure(c(0, 6.278, 7.345, 6.609, 6.141, 0, 4.399, 5.423, 4.787,
3.947, 0, 1.719, 3.422, 2.585, 2.308, 0, 0.891, 1.663, 1.018,
1.484, 0, 0.906, 0.742, 0.826, 1.406, 0, 1.798, 0.675, 0.743,
1.76, 0, 3.377, 2.871, 4.857, 3.19, 0, 6.42, 8.997, 9.13, 6.136
), .Dim = c(5L, 8L), .Dimnames = list(NULL, c("qout1", "qout2",
"qin1", "qin2", "qin3", "pin1", "pin2", "pin3"))
3-最终,被馈送到用于分析的“ grph $ try1”数据是这样的,前五行:
structure(list(Inputs = structure(c(3L, 1L, 2L, NA, NA), .Label =
c("Fertilizer", "Power", "Seed"), class = "factor"), Inputs2 =
structure(c(1L, 1L, 1L, NA, NA), .Label = "0", class = "factor"),
Inputs3 = structure(c(1L, 1L, 1L, NA, NA), .Label = "0", class =
"factor")), row.names = c("1", "2", "3", "NA", "NA.1"), class =
"data.frame")
答案 0 :(得分:1)
您的第一个问题是在计算assets.packages:
class: \Symfony\Component\Asset\Packages
arguments: ['@assets._default_package']
public: true
之前绘制绘图。您可以通过包含这样的grph$try1
语句来很容易地避免这种情况
req
您还希望将用户界面从output$coolplot <- renderPlotly({
req(!is.null(grph$try1))
p <- plot_ly(
...
)})
更改为plotOutput
。
由于您要访问data.frame中的不同输入,因此我认为最好将plotlyOutput
作为此类data.frame直接启动
grph$try1
您还需要添加isolate(grph$try1 <- data.frame(Inputs, Inputs2, Inputs3))
,否则,舍入函数现在不必在内部舍入它们了。情节叫最终看起来像这样
~
希望这会有所帮助!