在Shiny中使用rhandsontable时出错

时间:2017-05-09 17:55:01

标签: r shiny rhandsontable

我在一个闪亮的应用中观察到了rhandsontable的一些奇怪行为。在这个简单的示例中,如果发生某些事件,我会将data.frame分配给reactiveValues元素。然后,数据显示在rhandsontable中。但是当我更改表的某些条目时,函数hot_to_r将失败: seq.default中的错误:参数' length.out'必须长度为1

奇怪的是,只有当我使用iris时才会出现错误,但是当我使用iris[1:50, ]时,错误才会发生,这应该是相同的。有人有想法,如何解决这个问题?

values$data在点击NULL之前仍有actionButton时出现了其他错误。我已经意识到这一点,但这与此问题无关。)

library(shiny)

ui <- fluidPage(
  actionButton("click", "click"),
  rHandsontableOutput("table")
)

server <- function(input, output, session) {

  values <- reactiveValues(data = NULL)

  observeEvent(input$click, {
    values$data <- iris # with iris[1:50, ] no error appears
  })

  output$table <- renderRHandsontable({
    rhandsontable(t(values$data))
  })

  observe({
    if (!is.null(input$table$changes$changes)) {
      table_data <- hot_to_r(input$table)
      print(table_data)
    }
  })

}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

@BigDataScientist正在开展某项工作,colnames(t(iris))NULL,而colnames(t(iris[1:50,]))则不是。rhandsontable。这对我来说是一个谜,但防止这种无效可以解决你的问题。在rhandsontable(data.frame(t(values$data))) 的调用中使用某些东西应该可以解决问题。使用

package javaapplication1;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;





public class JavaApplication1 extends JFrame implements ActionListener  {

JPanel panel;
Container window = getContentPane(); 
ChartFrame cframe;
private ArrayList<Solar> sols;

public static void main(String[] args) throws IOException {

JavaApplication1 demo = new JavaApplication1();
demo.setPreferredSize(new Dimension(1000,500));
demo.createGUI();



demo.setVisible(true);
}

private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);

window.setLayout(new FlowLayout());
panel = new JPanel();
JButton button = new JButton("Get Forecast");

panel.setBackground(Color.white);
panel.setPreferredSize(new Dimension(1000,400));

//ChartFrame cframe = new ChartFrame("First", chart); 

window.add(panel);
window.add(button);
button.addActionListener(this);
pack();
    }

    @Override
public void actionPerformed(ActionEvent e) {
sols = new ArrayList();
BufferedReader crunchifyBuffer = null;
final String DELIMITER = ",";


    try {
        crunchifyBuffer = new BufferedReader(new FileReader("C:\\Users\\xxxx\\Documents\\Molar Forecasts\\fs-rad.20170421.21.csv"));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
    }
                String line = null;
    try {
        line = crunchifyBuffer.readLine(); // Read first line
    } catch (IOException ex) {
        Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
    }
if (line != null) { try {
    // Check it's not null
    //   line = crunchifyBuffer.readLine(); // Read second line
    while ((line = crunchifyBuffer.readLine()) != null)
    {
        //Get all tokens available in line
        String[] tokens = line.split(DELIMITER);
        Solar sol = new Solar(tokens[0], tokens[1], tokens[2], Double.parseDouble(tokens[3]));

        sols.add(sol);

    }   } catch (IOException ex) {
        Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
    }
processdata();
}}




    public void processdata() {
       System.out.println(sols.size());

 DefaultCategoryDataset dataset = new DefaultCategoryDataset();        

for (int index = 1 ; index <sols.size ();index++){
dataset.addValue(sols.get(index).details1(), "Molar Irradiation", (sols.get(index).details()));

}
JFreeChart chart = ChartFactory.createBarChart( 
"Bar Chart Demo", 
// chart title 
"Category", // domain axis label 
"Value", // range axis label 
dataset, // data 
PlotOrientation.VERTICAL, // orientation 
true, // include 
true, // 
false // URLs? 
);

// create and display a frame... 

add(panel);
pack(); 
//panel.add(cframe,BorderLayout.CENTER);
panel.validate();
//
//
setVisible(true);
    }
        }

为我工作。